//
//  HotCityView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/25.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol HotCityViewDelegate {
    func returnCityName(city:String)
}

class HotCityView: UIView {
    
    private let cityNum = 12
    private lazy var cityBtns = [UIButton]()
    private let citys = ["北京","上海","深圳","重庆","成都","南京","贵阳","杭州","沈阳","西安","广州","福州"]
    
    private let metroNum = 6
    private lazy var metroCityBtns = [UIButton]()
    private let metroCitys = ["保定","常州","东莞","杭州","佛山","南海"]
    
    // MARK: 代理
    public var delegate:HotCityViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        init_views()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        // 数量
        let columns = 3
        // 宽度
        let btnW = MARGIN100
        // 高度
        let btnH:CGFloat = 38
        // 间距
        let margin = (frame.width - CGFloat(columns) * btnW) / CGFloat(columns + 1)
        // 热门文本
        let hotText = UILabel(title: "热门", color: COLOR_NICE, size: 13)
        hotText.frame = CGRect(x: MARGIN, y: MARGIN, width: MARGIN40, height: MARGIN)
        addSubview(hotText)
        var tempHeight:CGFloat = 0
        // 热门城市
        for i in 0..<cityNum {
            // 坐标
            let btnY = CGFloat(i / columns) * (margin * 0.5 + btnH)
            let btnX = CGFloat(i % columns) * (margin + btnW) + margin
            // 摆放
            let city = cityBtns[i]
            city.frame = CGRect(x: btnX, y: btnY + MARGIN40, width: btnW, height: btnH)
            // 最后一个view高度
            if i == cityNum - 1 {
                tempHeight = city.frame.maxY + MARGIN
            }
        }
        // 地铁联运城市文本
        let metroText = UILabel(title: "地铁联运城市", color: COLOR_NICE, size: 13)
        metroText.frame = CGRect(x: MARGIN, y: tempHeight, width: MARGIN100, height: MARGIN)
        addSubview(metroText)
        // 地铁联运城市
        for i in 0..<metroNum {
            // 坐标
            let btnY = CGFloat(i / columns) * (margin * 0.5 + btnH)
            let btnX = CGFloat(i % columns) * (margin + btnW) + margin
            
            // 摆放
            let city = metroCityBtns[i]
            city.frame = CGRect(x: btnX, y: btnY + metroText.frame.maxY + MARGIN, width: btnW, height: btnH)
        }
    }
    
    @objc func clickItem(btn:UIButton) -> Void {
        if let title = btn.currentTitle {
            delegate?.returnCityName(city: title)
        }
    }
    
    private func init_views() -> Void {
        
        func getBtn() -> UIButton {
            let btn = UIButton()
            btn.layer.borderWidth = 1
            btn.layer.masksToBounds = true
            btn.layer.cornerRadius = 19
            btn.setTitleColor(COLOR_NICE, for: .normal)
            btn.layer.borderColor = UIColor.gray.cgColor
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
            // 添加点击事件
            btn.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
            return btn
        }
        // 热门城市
        for i in 0..<cityNum {
            let btn = getBtn()
            btn.setTitle(citys[i], for: .normal)
            cityBtns.append(btn)
            // 添加到父控件
            addSubview(btn)
        }
        // 地铁联运城市
        for i in 0..<metroNum {
            let btn = getBtn()
            btn.setTitle(metroCitys[i], for: .normal)
            metroCityBtns.append(btn)
            // 添加到父控件
            addSubview(btn)
        }
    }
}
